import { getToken, setToken } from '@/utils/auth'
import { sysLogin, sysProfile, sysUser } from '@/api/user.js'
const state = {
  token: getToken() || '',
  userInfo: '',
  res3: {}
}
const mutations = {
  // 保存token
  setToken(state, value) {
    state.token = value
    setToken(value)
  },
  // 保存用户信息
  setUserInfo(state, value) {
    state.userInfo = value
  },
  // 清空token和用户信息
  logout(state) {
    state.token = ''
    state.userInfo = ''
    setToken('')
  }
}
const actions = {
  async toLogin(store, form) {
    const res = await sysLogin(form)
    store.commit('setToken', res.data)
  },
  // 获取用户信息
  async getUserInfo({ commit }) {
    const res = await sysProfile()
    const res2 = await sysUser(res.data.userId)
    // 合并对象
    // const res3 = Object.assign({}, res.data, res2.data)
    const res3 = { ...res, ...res2 }
    commit('setUserInfo', res3)
    // console.log('用户信息', res3)
  }
}
const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
